
#include "CFont.h"

CFontManager::CFontManager(SDL_Surface **Screen)
{
	TTF_Init();
	m_Screen = Screen;
}

void CFontManager::LoadFont(std::string FontName, std::string FontFile, Uint32 Weight)
{
	TTF_Font* font = TTF_OpenFont(FontFile.c_str(), Weight);
	if (font)
		m_Fonts.insert(std::pair<std::string, TTF_Font*>(FontName, font));
}

void CFontManager::PrintText(std::string Font, std::string Text, float x, float y, 
				    Uint32 red, Uint32 green, Uint32 blue, 
				    float scale, float rotation, Uint32 alpha)
{
	std::map<std::string, TTF_Font*>::iterator itr = m_Fonts.find(Font);
	if (itr == m_Fonts.end())
		return;
	TTF_Font* ttf_font = itr->second;
	SDL_Color textColor;
	textColor.r = red; textColor.g = green; textColor.b = blue;
	SDL_Surface *text = TTF_RenderUTF8_Solid(ttf_font, Text.c_str(), textColor);
	if (scale == 1 && rotation == 0)
	{
		SDL_Rect rect;
		rect.x = (Sint16)x; rect.y = (Sint16)y;
		rect.w = (Uint16)text->w; (Uint16)rect.h = text->h;
		if (alpha != 255)
			SDL_SetAlpha(text, SDL_SRCALPHA, alpha);
		SDL_BlitSurface(text, NULL, *m_Screen, &rect);
	}
	else
	{
		SDL_Rect rect;
		SDL_Surface *textdest = rotozoomSurface(text, rotation, scale, 1);
		rect.x = (Sint16)x; rect.y = (Sint16)y;
		rect.w = (Uint16)textdest->w; rect.h = (Uint16)textdest->h;
		if (alpha != 255)
			SDL_SetAlpha(textdest, SDL_SRCALPHA, alpha);
		SDL_BlitSurface(textdest, NULL, *m_Screen, &rect);
		SDL_FreeSurface(textdest);
	}
	SDL_FreeSurface(text); 
}